package com.rjj.wlqq.tool.io;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @author renjianjun
 * @date 2020/8/13 13:15
 * @email 215060580@qq.com
 */
public class ResponseDownOutPutStream {

    /**
     * 下载的响应封装
     *
     * @param request  请求
     * @param response 响应
     * @param fileName 文件名称 a.txt
     * @return 响应输出流
     * @throws IOException io异常
     */
    public static OutputStream getResponseOutputStream(HttpServletRequest request, HttpServletResponse response, String fileName) throws IOException {
        //清空一下response对象，否则出现缓存什么的
        response.reset();
        response.addHeader("Cache-Control", "no-cache, no-store, must-revalidate");
        response.addHeader("charset", "utf-8");
        response.addHeader("Pragma", "no-cache");
        //指明这是一个下载的respond
        //response.setContentType("application/x-download,charset=utf-8");
        response.setContentType("application/force-download;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.addHeader("Content-Disposition", "attachment;" + FileKit.encodeFileName(request, fileName));
        return response.getOutputStream();
    }
}
